import 'package:base_lib/index.dart';
import 'package:doctor_login_lib/select_section/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:base_lib/dictionary/entity/medical_section_entity.dart'
    as SectionBean;

///选择科室
class SelectSectionPage
    extends BaseBlocPage<SelectSectionBloc, _SelectSectionPageState> {
  SelectSectionPage(Parameters parameters) : super(parameters);

  @override
  _SelectSectionPageState generateThemeState() {
    return _SelectSectionPageState();
  }
}

class _SelectSectionPageState extends BaseBlocPageState<SelectSectionBloc> {
  @override
  void initState() {
    super.initState();
    bloc.getSection(context);
    String sectionCode = parameters.getString("sectionCode") ?? " _ ";
    bloc.setSectionCode(sectionCode);
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text("选择科室"),
        actions: [
          FlatButton(
            onPressed: () {
              bloc.save(context);
            },
            child: Text(
              "保存",
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
      body: Container(
        color: Colors.white,
        child: Row(
          children: [
            Expanded(
              flex: 3,
              child: Container(
                color: Colors.grey[200],
                child: StreamBuilder(
                  stream: bloc.topSection,
                  initialData: null,
                  builder: (context,
                      AsyncSnapshot<List<SectionBean.Data>> snapshot) {
                    return snapshot.hasData
                        ? ListView.builder(
                            itemBuilder: (context, pos) {
                              var parent = snapshot.data[pos];
                              bool isCheck =
                                  parent.dictCode == bloc.checkedTopCode;
                              return GestureDetector(
                                onTap: () {
                                  bloc.getSection2(context, parent);
                                },
                                child: Container(
                                  color: isCheck
                                      ? Colors.white
                                      : theme.themeColor.themeBgColor,
                                  padding: EdgeInsets.symmetric(
                                      vertical: 15, horizontal: 10),
                                  child: Text(
                                    "${parent.dictName}",
                                    style: TextStyle(
                                        color: isCheck
                                            ? theme.themeColor.themeColor
                                            : Colors.grey),
                                  ),
                                ),
                              );
                            },
                            itemCount: snapshot.data.length,
                          )
                        : buildLoadingIndicator();
                  },
                ),
              ),
            ),
            Expanded(
              flex: 5,
              child: StreamBuilder(
                stream: bloc.childSection,
                initialData: null,
                builder:
                    (context, AsyncSnapshot<List<SectionBean.Data>> snapshot) {
                  return snapshot.hasData
                      ? ListView.builder(
                          itemBuilder: (context, pos) {
                            var child = snapshot.data[pos];
                            bool isCheck = false;
                            try {
                              isCheck = child.dictCode == bloc.checkedChildCode;
                            } catch (e) {
                              print(e);
                            }
                            return GestureDetector(
                              onTap: () {
                                bloc.setSelectedChildSection(child);
                              },
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                    vertical: 15, horizontal: 15),
                                child: Text(
                                  child.dictName,
                                  style: TextStyle(
                                      color: isCheck
                                          ? theme.themeColor.themeColor
                                          : Colors.grey),
                                ),
                              ),
                            );
                          },
                          itemCount: snapshot.data.length,
                        )
                      : SizedBox();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  getSection(MTheme theme) async {
    bloc.getSection(context);
  }

  @override
  SelectSectionBloc generateBloc() {
    return SelectSectionBloc();
  }
}
